/*
 * Copyright 2007-2010 VTT Biotechnology
 * This file is part of DP.
 *
 * DP is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * DP is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * DP; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
 * Fifth Floor, Boston, MA 02110-1301 USA
 */
package PD.modules.file.openFiles;

import PD.data.ParameterSet;
import PD.desktop.Desktop;
import PD.desktop.PDMenu;
import PD.main.PDCore;
import PD.main.PDModule;
import PD.taskcontrol.Task;
import PD.taskcontrol.TaskListener;
import PD.taskcontrol.TaskStatus;
import PD.util.dialogs.ExitCode;
import PD.util.dialogs.ParameterSetupDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.logging.Logger;

/**
 *
 * @author scsandra
 */
public class OpenFile implements PDModule, TaskListener, ActionListener {

        private Logger logger = Logger.getLogger(this.getClass().getName());
        private Desktop desktop;
        private OpenFileParameters parameters;

        public void initModule() {
                this.desktop = PDCore.getDesktop();
                this.parameters = new OpenFileParameters();
                desktop.addMenuItem(PDMenu.FILE, "Open Files..",
                        "Opens training of file", KeyEvent.VK_O, this, null, null);
        }

        public void taskStarted(Task task) {
                logger.info("Running Open Files");
        }

        public void taskFinished(Task task) {
                if (task.getStatus() == TaskStatus.FINISHED) {
                        logger.info("Finished Open Files on " + ((OpenBasicFileTask) task).getTaskDescription());
                }

                if (task.getStatus() == TaskStatus.ERROR) {

                        String msg = "Error while Open Files on .. " + ((OpenBasicFileTask) task).getErrorMessage();
                        logger.severe(msg);
                        desktop.displayErrorMessage(msg);

                }
        }

        public void actionPerformed(ActionEvent e) {
                ExitCode exitCode = setupParameters();
                if (exitCode != ExitCode.OK) {
                        return;
                }

                runModule();
        }

        public ExitCode setupParameters() {
                ParameterSetupDialog dialog = new ParameterSetupDialog("LCMS Table View parameters", parameters);
                dialog.setVisible(true);
                return dialog.getExitCode();
        }

        public ParameterSet getParameterSet() {
                return parameters;
        }

        public void setParameters(ParameterSet parameterValues) {
                this.parameters = (OpenFileParameters) parameterValues;
        }

        public String toString() {
                return "Open Files";
        }

        public Task[] runModule() {

                // prepare a new group of tasks

                Task tasks[] = new OpenBasicFileTask[1];
                tasks[0] = new OpenBasicFileTask(parameters, desktop);

                PDCore.getTaskController().addTasks(tasks);

                return tasks;

        }
}
